import pymysql

try:
    connect = pymysql.connect(
        host='119.45.203.8',  # 主机名
        user='root',  # 数据库用户名
        password='zsy123456',  # 数据库密码
        db='pythondemo',  # 数据库名
        charset='utf8',  # 字符集编码
        cursorclass=pymysql.cursors.DictCursor,  # 游标类型,
        port=3301  # 端口
    )
except Exception as e:
    print(e)


try:
    # 获取游标对象
    with connect.cursor() as cursor:
        # SQL语句
        sql = """
        create table test(
        id int auto_increment primary key,
        name varchar(255) not null
        );
        """
        # 1.执行SQL语句
        cursor.execute(sql)

        # 用于批量操作，比如批量更新
        # cursor.executemany()

        # 获取查询结果集中的下一条记录
        rst =  cursor.fetchone()
        print("rst--->", rst)

        # 获取指定数量的记录
        # cursor.fetchmany()

        # 获取结构集的所有记录
        # cursor.fetchall()

        # 提交操作
        connect.commit()

except Exception as e:
    print(e)
finally:
    # 关闭连接
    connect.close()




